package com.example.userservice.service.impl;

import com.example.userservice.dao.UserMapper;
import com.example.userservice.model.User;
import com.example.userservice.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;

    @Resource
    RestTemplate restTemplate;

    @Override
    //    判断用户名是否已经存在
    public int ifUserName(String userName){
        //        用户名具有唯一性，判断要增加的用户名是否已经存在
        int num = userMapper.selUserName(userName);
        if(num != 0){
            restTemplate.getForObject("http://log-service/log/saveLog?logTarget="+userName+"&logLevel=error"+"&logDetail="+"新增用户"+userName+"失败，用户名已存在",String.class);
            return 0;
        }
        return 1;
    }

//    新增用户
    @Override
    public int addUser(String userName, String userSecret, String userShowName, String userDetail){

        Date date = date();

        String userId = getUser();

        User user = new User(userId,userName,userSecret,userShowName,userDetail,date, date);

        int num = userMapper.addUser(user);

        restTemplate.getForObject("http://log-service/log/saveLog?logTarget="+userName+"&logLevel=info"+"&logDetail="+"新增用户"+userName+"成功",String.class);

        return num;

    }

//    确定用户id
    public String getUser(){
        //        查询用户数量
        int num = userMapper.userCount();

//        初始化用户id
        String userId = "1";

//        当数据库中用户存在时
        if(num != 0){
            int num1 = userMapper.userIdMax();
            int userId1 = Integer.parseInt(userId)+num1;
            userId = Integer.toString(userId1);
        }
        return userId;
    }

    @Override
    //    根据输入的用户名模糊查询
    public List<User> selUserByName(String userName){
        return userMapper.selUserByName(userName);
    }


    @Override
    //    显示所有的用户信息
    public List<User> allUser(String userName){
        return userMapper.allUser(userName);
    }

    @Override

//    批量删除用户
    public int delUsers(List<String> userName){
        int num = userMapper.delUsers(userName);
        for(String userN : userName){
            restTemplate.getForObject("http://log-service/log/saveLog?logTarget="+userN+"&logLevel=info"+"&logDetail="+"删除用户"+userN+"成功",String.class);
        }
        return num;
    }

    @Override
    //    显示用户信息
    public User showUser(String userId){
        return userMapper.showUser(userId);
    }

    @Override
    //    修改用户信息功能
    public int updateUser(String userName,String userSecret, String userShowName, String userDetail){
        Date updateTime = date();
        userMapper.updateUser(userName,userSecret,userShowName,userDetail,updateTime);
        restTemplate.getForObject("http://log-service/log/saveLog?logTarget="+userName+"&logLevel=info"+"&logDetail="+"修改用户"+userName+"信息成功",String.class);
        return 1;
    }

    public Date date(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dFormat = sdf.format(date);
        return Timestamp.valueOf(dFormat);
    }
}
